package org.scau.studentevaluation.utils;

import java.lang.reflect.Field;
import java.util.Arrays;
import java.util.HashMap;
import java.util.stream.Collectors;

/**
 * @ClassName: StringUtils
 * @Description: TODO
 * @Author: sunslikes
 * @Date: 2020/9/3 3:41
 * @Version: 1.0
 */
public class StringUtils {
    public static <T> String toString(T t) {
        HashMap<Field, Object> propertiesMap = new HashMap<>();
        StringBuilder stringBuilder = new StringBuilder();
        Class clazz = t.getClass();
        stringBuilder.append(clazz.getSimpleName());
        Field[] fields = clazz.getDeclaredFields();
        stringBuilder.append(Arrays.stream(fields).map(field -> {
            StringBuilder temp = new StringBuilder();
            temp.append(field.getName());
            temp.append("=");
            field.setAccessible(true);
            try {
                temp.append(field.get(t));
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
            return temp.toString();
        }).collect(Collectors.joining(", ", "[", "]")))
        ;
        return stringBuilder.toString();
    }
}
